package com.inns.position.controller;

import com.alibaba.fastjson.JSON;
import com.inns.position.hbase.HbaseConsts;
import com.inns.position.hbase.service.IHBaseService;
import com.inns.position.kafka.bean.LocationInfo;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by phoenix on 2018/4/7.
 */
@RestController
public class LocationInfoController {

    @Resource
    private IHBaseService ihBaseService;

    @GetMapping("userInfo")
    public LocationInfo userInfo(String IMSI) {
        List<List<Cell>> cells = ihBaseService.scanRegexRowKey(HbaseConsts.TableName_Original, IMSI);
        if (cells != null && cells.size() > 0) {
            String value = "";
            List<Cell> cells1 = cells.get(0);
            for (Cell cell : cells1) {
                String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                String cValue = Bytes.toString(CellUtil.cloneValue(cell));
                String st = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.printf("rowKey=%s,family=%s,qualifier=%s,value=%s\n", rowKey, family, qualifier, cValue);
                if (qualifier.equals("value")) {
                    value = cValue;
                }
            }
            LocationInfo locationInfo = JSON.parseObject(value, LocationInfo.class);
            return locationInfo;
        }
        return null;
    }
}
